package com.liu.efk.zdcq.controller;

import com.liu.common.domain.ResultVo;
import com.liu.common.utils.JacksonUtil;
import com.liu.efk.zdcq.pojo.ClientManager;
import com.liu.efk.zdcq.pojo.CollectPojo;
import com.liu.efk.zdcq.sevice.CollectService;
import com.liu.efk.zdcq.sevice.UpDateService;
import com.liu.efk.zdcq.util.DateTimeUtil;
import com.liu.efk.zdcq.util.TaskIDGen;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by shi860715@126.com
 * Date 2021/5/8 12:34
 * Description
 */
@RestController
@Slf4j
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    UpDateService upDateService;

    @Autowired
    CollectService collectService;


    @PostMapping("/{dataID}")
    public ResultVo collect(@PathVariable String  dataID, @RequestBody CollectPojo request){


    String  result =collectService.sendcommd(dataID,request);
    System.out.println(result);
     return  ResultVo.success("发送成功！！！");


    }

    @GetMapping("/update")
    public ResultVo updateCollect( @RequestBody CollectPojo request) {
        Channel channel = ClientManager.getClientChannel(request.getAddress());

        TaskIDGen taskIDGen = TaskIDGen.getInstance();
        String id = taskIDGen.getTaskId();
        log.info(id);
        String base64="";
        try {
            base64= upDateService.updateSoft();
        }catch (Exception e){
            log.info("转码失败");
        }
     String  task="###<TaskID="+id+";TaskFlag=1;TaskContent{ObjectAddress="+request.getAddress()+";CommandStyle=3;DataID=1;DataContent<VersionType=SV120210419;FrmTotal=1;FrmIdx=1;Data="+base64+">}>###";
      log.info("发送升级命令"+task);
        channel.writeAndFlush(task);

        return ResultVo.success("升级程序");
    }



    @GetMapping("/hello")
    public ResultVo hello(){
        return ResultVo.success("服务组建~~~~");
    }
}
